#coding=utf-8
import pygame
from memory_pic import *
from base64 import *
from random import *


def get_pic(pic_code, pic_name):
    image = open(pic_name, 'wb')
    image.write(b64decode(pic_code))
    image.close()


get_pic(me_destroy_1_png, "me_destroy_1.png")
get_pic(me_destroy_2_png, "me_destroy_2.png")
get_pic(me_destroy_3_png, "me_destroy_3.png")
get_pic(me_destroy_4_png, "me_destroy_4.png")
get_pic(me1_png, "me1.png")
get_pic(me2_png, "me2.png")


class MyPlane(pygame.sprite.Sprite):

    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)
        self.image1 = pygame.image.load('me1.png').convert_alpha()
        self.image2 = pygame.image.load('me2.png').convert_alpha()
        self.image3 = pygame.image.load('me_destroy_1.png').convert_alpha()
        self.image4 = pygame.image.load('me_destroy_2.png').convert_alpha()
        self.image5 = pygame.image.load('me_destroy_3.png').convert_alpha()
        self.image6 = pygame.image.load('me_destroy_4.png').convert_alpha()
        self.destroy_images = []
        self.destroy_images.extend([self.image3, self.image4, self.image5, self.image6])
        self.rect = self.image1.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.top = bg_size[0] - self.rect.width - 10, self.height - self.rect.height - 60
        self.speed = 3
        self.speed_range = 12
        self.life_num = 3
        self.active = True
        self.invincible = False
        self.mask = pygame.mask.from_surface(self.image1)

    def moveUp(self, offset):
        if self.rect.top > 0:
            speed = self.speed + offset / self.speed_range
            self.rect.top -= speed 
        else:
            self.rect.top = 0

    def moveDown(self, offset):
        if self.rect.bottom < self.height - 60:
            speed = self.speed + offset / self.speed_range
            self.rect.top += speed
        else:
            self.rect.bottom = self.height - 60

    def moveLeft(self, offset):
        if self.rect.left > 0:
            speed = self.speed + offset / self.speed_range
            self.rect.left -= speed
        else:
            self.rect.left = 0

    def moveRight(self, offset):
        if self.rect.right < self.width:
            speed = self.speed + offset / self.speed_range
            self.rect.left += speed
        else:
            self.rect.right = self.width

    def still(self):
        pass

    def reset(self, bg_size):
        self.rect.left, self.rect.top = bg_size[0] - self.rect.width - 10, self.height - self.rect.height - 60
        self.active = True
        self.invincible = True


class MyPlane_Mate(pygame.sprite.Sprite):

    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)
        self.image1 = pygame.image.load('me1.png').convert_alpha()
        self.image2 = pygame.image.load('me2.png').convert_alpha()
        self.image3 = pygame.image.load('me_destroy_1.png').convert_alpha()
        self.image4 = pygame.image.load('me_destroy_2.png').convert_alpha()
        self.image5 = pygame.image.load('me_destroy_3.png').convert_alpha()
        self.image6 = pygame.image.load('me_destroy_4.png').convert_alpha()
        self.destroy_images = []
        self.destroy_images.extend([self.image3, self.image4, self.image5, self.image6])
        self.rect = self.image1.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.top = 20, self.height - self.rect.height - 60
        self.speed = 3
        self.speed_range = 12
        self.life_num = 3
        self.active = True
        self.invincible = False
        self.mask = pygame.mask.from_surface(self.image1)

    def moveUp(self, offset):
        if self.rect.top > 0:
            speed = self.speed + offset / self.speed_range
            self.rect.top -= speed
        else:
            self.rect.top = 0

    def moveDown(self, offset):
        if self.rect.bottom < self.height - 60:
            speed = self.speed + offset / self.speed_range
            self.rect.top += speed
        else:
            self.rect.bottom = self.height - 60

    def moveLeft(self, offset):
        if self.rect.left > 0:
            speed = self.speed + offset / self.speed_range
            self.rect.left -= speed
        else:
            self.rect.left = 0

    def moveRight(self, offset):
        if self.rect.right < self.width:
            speed = self.speed + offset / self.speed_range
            self.rect.left += speed
        else:
            self.rect.right = self.width

    def still(self):
        pass

    def reset(self):
        self.rect.left, self.rect.top = 20, self.height - self.rect.height - 60
        self.active = True
        self.invincible = True


class Plane_Gif(pygame.sprite.Sprite):

    def __init__(self, bg_size):
        self.image = pygame.image.load('me1.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = randint(20, bg_size[0] - self.rect.width - 10), randint(0, bg_size[1] - self.rect.height - 10)
        self.speed = [randint(3, 7), randint(3, 7)] #两个方向上速度的绝对值
        self.side = [choice([-1, 1]), choice([-1, 1])]#速度方向
        self.width, self.height = bg_size[0], bg_size[1]
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        self.rect = self.rect.move((self.side[0] * self.speed[0], self.side[1] * self.speed[1]))
        if self.rect.right <= 0:
            self.rect.left = self.width
        elif self.rect.left >= self.width:
            self.rect.right = 0
        elif self.rect.bottom <= 0:
            self.rect.top = self.height
        elif self.rect.top >= self.height: 
            self.rect.bottom = 0